<?
/**
* <b>htmlRenderer</b>
*
* Html-rendering class
*
* @package gravy.classes.renderers
* @class htmlRenderer extends renderer
*/
class htmlRenderer extends renderer{
	public $gravy; //why is this declared here again if its arleady in base class?

	/**
	* <b>__construct</b>
	*
	* Calls base class's (renderer) construct
	*
	* @fn __construct
	* @param $gravy - gravy (application) instance
	* @return void
	*/
	public function __construct($gravy){
		// call the construct
		parent::__construct($gravy);
	}

	/**
	* <b>setHeadersForOutput</b>
	* Implementation of abstract function
	* Sets up the header information for HTML
	*
	* @fn setHeadersForOutput
	* @param $statusCode [def = null]
	* @return void
	*/
	public function setHeadersForOutput($statusCode=null){
		if(!$this->headerSent){
			header("Cache-control: private");
			header("Content-Type: text/html; charset=utf-8");
			if($statusCode!=null){
				header(' ',true,$statusCode);
			}
			$this->headerSent=true;
			if($this->gravy->data["raw"]===TRUE){
				print($this->gravy->data["data"]);
				exit();
			}
		}
	}

	/**
	* <b>render</b>
	*
	* Sets output headers (base), gets bunch of stuff (validator, errors, templateData, doc var's, gravy
	* Includes helpers
	* if no fatal errors, include the theme file for display in the browser
	*
	* @fn render
	* @return void
	*/
	public function render(){
		// start the render process
		$this->setHeadersForOutput();

		$VALIDATOR=$this->gravy->validator;
 		$ERRORS=$this->gravy->errors;
		$DATA=$this->gravy->getTemplateData();
		$DOCUMENT=$this->getDocumentVariables();
		$GRAVY=$this->gravy;

		/**
 		* Include helper (utility) functions: helper_form, helper_html
 		*/
 		require_once(GRAVY_ROOT_DIR."functions/helpers.php");

 		/**
 		* If no fatal errors, include the theme_file which will render the application further
 		*/
		if($this->gravy->isFatalError){
			$this->renderError(404);
		}else{
			$IS_FATAL_ERROR=false;

			require_once($DOCUMENT["theme_file"]);
		}
	}

	/**
	* <b>renderError</b>
	*
	* Display error theme file with error that occured
	*
	* @fn renderError
	* @param $statusCode
	* @return void
	*/
	public function renderError($statusCode){
		$VALIDATOR=$this->gravy->validator;
		$ERRORS=$this->gravy->errors;
		$DATA=$this->gravy->getTemplateData();
		$DOCUMENT=$this->getDocumentVariables();
		$GRAVY=$this->gravy;

		#$this->setHeadersForOutput($statusCode);
		$errorThemeFile=THEMES_ROOT_DIR.$this->gravy->error_theme.".".TEMPLATE_EXT;
		if(file_exists($errorThemeFile)){
			$IS_FATAL_ERROR=$this->gravy->isFatalError;
			require_once($errorThemeFile);
		}else{
			$this->gravy->debug("errorTheme",$errorThemeFile);
			die("ERROR $statusCode: No Templates Found!");
			exit();
		}
	}

	/**
	* <b>getDocumentVariables</b>
	*
	* Gets and returns theme and controller files
	*
	* @fn getDocumentVariables
	* @return array($result)
	*/
	public function getDocumentVariables(){
		$result=array();

		$result["theme"]=$this->gravy->theme;
		$result["theme_file"]=THEMES_ROOT_DIR.$this->gravy->theme.".".TEMPLATE_EXT;

		$result["controller"]=$this->gravy->controller;
		$result["controller_file"]=VIEWS_ROOT_DIR.$this->gravy->view."/".$this->gravy->controller.".".TEMPLATE_EXT;

		$result["title"]=$this->gravy->pageTitle;

		$result["description"]=$this->gravy->pageDescription;

		$result["keywords"]=$this->gravy->pageKeywords;

		$cache=$this->gravy->config->CACHE;
		if(is_string($cache)){
			$result["cache"]=$cache;
		}elseif($cache){
			$result["cache"]="";
		}else{
			$result["cache"]=($this->gravy->config->CACHE)?"r=":"r=".rand(0,10000);
		}

		return($result);
	}
};
?>